package com.ruiben.mall.controller;


import com.ruiben.mall.LoginRequired;
import com.ruiben.mall.impl.RechargeCardServiceImpl;
import com.ruiben.mall.utils.CommonJSONResult;
import com.ruiben.mall.utils.PagedResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/card")
public class RechargeCardController extends BaseController {


    @Autowired
    private RechargeCardServiceImpl rechargeCardService;



    /**
     * 用户充值
     * @param cardNumber
     * @param cardPassword
     * @return
     */
    @PostMapping("userRecharge")
    public CommonJSONResult userRecharge(String cardNumber,String cardPassword,String rechargeUser){

        if (StringUtils.isBlank(rechargeUser)){
            return CommonJSONResult.errorMsg("用户id不能为空");
        }

        if (StringUtils.isBlank(cardNumber)){
            return CommonJSONResult.errorMsg("充值卡号不能为空");
        }

        if (StringUtils.isBlank(cardPassword)){
            return CommonJSONResult.errorMsg("充值密码不能为空");
        }


        boolean b = rechargeCardService.userRecharge(rechargeUser, cardNumber, cardPassword);
        if (b){
            return CommonJSONResult.ok();
        }
        return CommonJSONResult.errorMsg("充值失败");
    }

    /**
     * 获取充值记录
     * @param page
     * @return
     */
    @PostMapping("getRechargeLogList")
    public CommonJSONResult getRechargeLogList(Integer page){
        if (page == null){
            page = 1;
        }

        PagedResult rechargeLogList = rechargeCardService.getRechargeLogList(page, PAGE_SIZE);
        return CommonJSONResult.ok(rechargeLogList);

    }
}
